package com.sky.service.admin.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.mapper.admin.OrderMapper;
import com.sky.mapper.admin.OrdersDishMapper;
import com.sky.pojo.Courier;
import com.sky.pojo.OrderDetail;
import com.sky.pojo.Orders;
import com.sky.result.PageResult;
import com.sky.service.admin.OrderService;
import com.sky.vo.OrderStatisticsVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrdersDishMapper ordersDishMapper;

    @ApiOperation("取消订单")
    @Override
    public void putOrder(OrdersCancelDTO ordersCancelDTO) {
        orderMapper.deleteById(ordersCancelDTO.getId());
    }

    @Override
    public PageResult pageOrder(OrdersPageQueryDTO pageQueryDTO) {

        PageHelper.startPage(pageQueryDTO.getPage(),pageQueryDTO.getPageSize());
        Page<Orders> orders =orderMapper.pageOrder(pageQueryDTO);

        return new PageResult(orders.getTotal(),orders.getResult());
    }

    @Override
    public Courier receiveOrder(OrdersConfirmDTO confirmDTO) {

        return orderMapper.receiveOrder(confirmDTO);
    }

    @Override
    public Orders selectById(Long id) {
       Orders  orders= orderMapper.selectById(id);
        List<OrderDetail> detailList = orders.getOrderDetailList();

       OrderDetail orderDetail= ordersDishMapper.selectByOrdersId(orders.getId());
        BeanUtils.copyProperties(orderDetail,detailList);

        return orders;
    }

    @Override
    public OrderStatisticsVO orderStatusCount() {

       Integer Waiting4MerchantReceiveOrders= orderMapper.counstOrderStatus(Orders.TO_BE_CONFIRMED);
       Integer Waitng4RiderReceiveOrders=orderMapper.counstOrderStatus(Orders.CONFIRMED);
       Integer ToBeDeliveryOnProgressOrders=orderMapper.counstOrderStatus(Orders.TOBEPICKEDUP);
       Integer ToBeArrivedOrders=orderMapper.counstOrderStatus(Orders.TOBEDELIVERED);
       Integer CompletedOrders=orderMapper.counstOrderStatus(Orders.COMPLETE_ORDER);
       Integer CanceledOrders=orderMapper.counstOrderStatus(Orders.CANCELLED_ORDER);

      OrderStatisticsVO statisticsVO = OrderStatisticsVO.builder().
               waiting4MerchantReceiveOrders(Waiting4MerchantReceiveOrders).
               waitng4RiderReceiveOrders(Waitng4RiderReceiveOrders).
               toBeDeliveryOnProgressOrders(ToBeDeliveryOnProgressOrders).
               toBeArrivedOrders(ToBeArrivedOrders).
               completedOrders(CompletedOrders).
               canceledOrders(CanceledOrders).build();

        return statisticsVO;
    }


}
